package shop.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import shop.DAO.UserDAO;
import shop.bean.UserBean;

public class RegisterServlet extends HttpServlet{


	public void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, java.io.IOException {

		if(request.getParameter("name") != null 
				&& request.getParameter("username") != null
				&& request.getParameter("password") != null
				&& request.getParameter("zipcode") != null
				&& request.getParameter("street_address") != null
				&& request.getParameter("city") != null
				&& request.getParameter("country") != null
				){
			try{
				UserBean o = new UserBean();
				o.setName((String) request.getParameter("name"));
				o.setUserName((String) request.getParameter("username"));
				o.setCity((String) request.getParameter("city"));
				o.setCountry(request.getParameter("country"));
				o.setStreetAddress(request.getParameter("street_address"));
				o.setZipcode((String) request.getParameter("zipcode"));
				o.setPassword((String) request.getParameter("password"));

				UserDAO.newUser(o);
				response.sendRedirect("login");
			}
			catch (Throwable theException){System.out.println(theException);}
		}
		else{
			doGet(request, response);
		}
	}


	public void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, java.io.IOException {


		request.getRequestDispatcher("register.jsp").forward(request,response);	

	}
}
